﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using HslCommunication.BasicFramework;

namespace HslCommunication.Profinet.AllenBradley
{
	/// <summary>
	/// AB PLC的辅助类，用来辅助生成基本的指令信息
	/// </summary>
	// Token: 0x020000B6 RID: 182
	public class AllenBradleyHelper
	{
		// Token: 0x06000D33 RID: 3379 RVA: 0x0004DDD0 File Offset: 0x0004BFD0
		private static byte[] BuildRequestPathCommand(string address, bool isConnectedAddress = false)
		{
			byte[] result;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				string[] array = address.Split(new char[]
				{
					'.'
				}, StringSplitOptions.RemoveEmptyEntries);
				for (int i = 0; i < array.Length; i++)
				{
					string text = string.Empty;
					int num = array[i].IndexOf('[');
					int num2 = array[i].IndexOf(']');
					bool flag = num > 0 && num2 > 0 && num2 > num;
					if (flag)
					{
						text = array[i].Substring(num + 1, num2 - num - 1);
						array[i] = array[i].Substring(0, num);
					}
					memoryStream.WriteByte(145);
					byte[] bytes = Encoding.UTF8.GetBytes(array[i]);
					memoryStream.WriteByte((byte)bytes.Length);
					memoryStream.Write(bytes, 0, bytes.Length);
					bool flag2 = bytes.Length % 2 == 1;
					if (flag2)
					{
						memoryStream.WriteByte(0);
					}
					bool flag3 = !string.IsNullOrEmpty(text);
					if (flag3)
					{
						string[] array2 = text.Split(new char[]
						{
							','
						}, StringSplitOptions.RemoveEmptyEntries);
						for (int j = 0; j < array2.Length; j++)
						{
							int num3 = Convert.ToInt32(array2[j]);
							bool flag4 = num3 < 256 && !isConnectedAddress;
							if (flag4)
							{
								memoryStream.WriteByte(40);
								memoryStream.WriteByte((byte)num3);
							}
							else
							{
								memoryStream.WriteByte(41);
								memoryStream.WriteByte(0);
								memoryStream.WriteByte(BitConverter.GetBytes(num3)[0]);
								memoryStream.WriteByte(BitConverter.GetBytes(num3)[1]);
							}
						}
					}
				}
				result = memoryStream.ToArray();
			}
			return result;
		}

		/// <summary>
		/// 从生成的报文里面反解出实际的数据地址，不支持结构体嵌套，仅支持数据，一维数组，不支持多维数据
		/// </summary>
		/// <param name="pathCommand">地址路径报文</param>
		/// <returns>实际的地址</returns>
		// Token: 0x06000D34 RID: 3380 RVA: 0x0004DFA0 File Offset: 0x0004C1A0
		public static string ParseRequestPathCommand(byte[] pathCommand)
		{
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < pathCommand.Length; i++)
			{
				bool flag = pathCommand[i] == 145;
				if (flag)
				{
					string text = Encoding.UTF8.GetString(pathCommand, i + 2, (int)pathCommand[i + 1]).TrimEnd(new char[1]);
					stringBuilder.Append(text);
					int num = 2 + text.Length;
					bool flag2 = text.Length % 2 == 1;
					if (flag2)
					{
						num++;
					}
					bool flag3 = pathCommand.Length > num + i;
					if (flag3)
					{
						bool flag4 = pathCommand[i + num] == 40;
						if (flag4)
						{
							stringBuilder.Append(string.Format("[{0}]", pathCommand[i + num + 1]));
						}
						else
						{
							bool flag5 = pathCommand[i + num] == 41;
							if (flag5)
							{
								stringBuilder.Append(string.Format("[{0}]", BitConverter.ToUInt16(pathCommand, i + num + 2)));
							}
						}
					}
					stringBuilder.Append(".");
				}
			}
			bool flag6 = stringBuilder[stringBuilder.Length - 1] == '.';
			if (flag6)
			{
				stringBuilder.Remove(stringBuilder.Length - 1, 1);
			}
			return stringBuilder.ToString();
		}

		/// <summary>
		/// 获取枚举PLC数据信息的指令
		/// </summary>
		/// <param name="startInstance">实例的起始地址</param>
		/// <returns>结果数据</returns>
		// Token: 0x06000D35 RID: 3381 RVA: 0x0004E0E0 File Offset: 0x0004C2E0
		public static byte[] GetEnumeratorCommand(ushort startInstance)
		{
			return new byte[]
			{
				85,
				3,
				32,
				107,
				37,
				0,
				BitConverter.GetBytes(startInstance)[0],
				BitConverter.GetBytes(startInstance)[1],
				2,
				0,
				1,
				0,
				2,
				0
			};
		}

		/// <summary>
		/// 获取获得结构体句柄的命令
		/// </summary>
		/// <param name="symbolType">包含地址的信息</param>
		/// <returns>命令数据</returns>
		// Token: 0x06000D36 RID: 3382 RVA: 0x0004E14C File Offset: 0x0004C34C
		public static byte[] GetStructHandleCommand(ushort symbolType)
		{
			byte[] array = new byte[18];
			byte[] bytes = BitConverter.GetBytes(symbolType);
			bytes[1] = (bytes[1] & 15);
			array[0] = 3;
			array[1] = 3;
			array[2] = 32;
			array[3] = 108;
			array[4] = 37;
			array[5] = 0;
			array[6] = bytes[0];
			array[7] = bytes[1];
			array[8] = 4;
			array[9] = 0;
			array[10] = 4;
			array[11] = 0;
			array[12] = 5;
			array[13] = 0;
			array[14] = 2;
			array[15] = 0;
			array[16] = 1;
			array[17] = 0;
			return array;
		}

		/// <summary>
		/// 获取结构体内部数据结构的方法
		/// </summary>
		/// <param name="symbolType">地址</param>
		/// <param name="structHandle">句柄</param>
		/// <returns>指令</returns>
		// Token: 0x06000D37 RID: 3383 RVA: 0x0004E1D0 File Offset: 0x0004C3D0
		public static byte[] GetStructItemNameType(ushort symbolType, AbStructHandle structHandle)
		{
			byte[] array = new byte[14];
			ushort value = (ushort)(structHandle.TemplateObjectDefinitionSize * 4U - 21U);
			byte[] bytes = BitConverter.GetBytes(symbolType);
			bytes[1] = (bytes[1] & 15);
			byte[] bytes2 = BitConverter.GetBytes(0);
			byte[] bytes3 = BitConverter.GetBytes(value);
			array[0] = 76;
			array[1] = 3;
			array[2] = 32;
			array[3] = 108;
			array[4] = 37;
			array[5] = 0;
			array[6] = bytes[0];
			array[7] = bytes[1];
			array[8] = bytes2[0];
			array[9] = bytes2[1];
			array[10] = bytes2[2];
			array[11] = bytes2[3];
			array[12] = bytes3[0];
			array[13] = bytes3[1];
			return array;
		}

		/// <summary>
		/// 将CommandSpecificData的命令，打包成可发送的数据指令
		/// </summary>
		/// <param name="command">实际的命令暗号</param>
		/// <param name="session">当前会话的id</param>
		/// <param name="commandSpecificData">CommandSpecificData命令</param>
		/// <returns>最终可发送的数据命令</returns>
		// Token: 0x06000D38 RID: 3384 RVA: 0x0004E270 File Offset: 0x0004C470
		public static byte[] PackRequestHeader(ushort command, uint session, byte[] commandSpecificData)
		{
			byte[] array = new byte[commandSpecificData.Length + 24];
			Array.Copy(commandSpecificData, 0, array, 24, commandSpecificData.Length);
			BitConverter.GetBytes(command).CopyTo(array, 0);
			BitConverter.GetBytes(session).CopyTo(array, 4);
			BitConverter.GetBytes((ushort)commandSpecificData.Length).CopyTo(array, 2);
			return array;
		}

		/// <summary>
		/// 打包生成一个请求读取数据的节点信息，CIP指令信息
		/// </summary>
		/// <param name="address">地址</param>
		/// <param name="length">指代数组的长度</param>
		/// <param name="isConnectedAddress">是否是连接模式下的地址，默认为false</param>
		/// <returns>CIP的指令信息</returns>
		// Token: 0x06000D39 RID: 3385 RVA: 0x0004E2CC File Offset: 0x0004C4CC
		public static byte[] PackRequsetRead(string address, int length, bool isConnectedAddress = false)
		{
			byte[] array = new byte[1024];
			int num = 0;
			array[num++] = 76;
			num++;
			byte[] array2 = AllenBradleyHelper.BuildRequestPathCommand(address, isConnectedAddress);
			array2.CopyTo(array, num);
			num += array2.Length;
			array[1] = (byte)((num - 2) / 2);
			array[num++] = BitConverter.GetBytes(length)[0];
			array[num++] = BitConverter.GetBytes(length)[1];
			byte[] array3 = new byte[num];
			Array.Copy(array, 0, array3, 0, num);
			return array3;
		}

		/// <summary>
		/// 打包生成一个请求读取数据片段的节点信息，CIP指令信息
		/// </summary>
		/// <param name="address">节点的名称 -&gt; Tag Name</param>
		/// <param name="startIndex">起始的索引位置，以字节为单位 -&gt; The initial index position, in bytes</param>
		/// <param name="length">读取的数据长度，一次通讯总计490个字节 -&gt; Length of read data, a total of 490 bytes of communication</param>
		/// <returns>CIP的指令信息</returns>
		// Token: 0x06000D3A RID: 3386 RVA: 0x0004E34C File Offset: 0x0004C54C
		public static byte[] PackRequestReadSegment(string address, int startIndex, int length)
		{
			byte[] array = new byte[1024];
			int num = 0;
			array[num++] = 82;
			num++;
			byte[] array2 = AllenBradleyHelper.BuildRequestPathCommand(address, false);
			array2.CopyTo(array, num);
			num += array2.Length;
			array[1] = (byte)((num - 2) / 2);
			array[num++] = BitConverter.GetBytes(length)[0];
			array[num++] = BitConverter.GetBytes(length)[1];
			array[num++] = BitConverter.GetBytes(startIndex)[0];
			array[num++] = BitConverter.GetBytes(startIndex)[1];
			array[num++] = BitConverter.GetBytes(startIndex)[2];
			array[num++] = BitConverter.GetBytes(startIndex)[3];
			byte[] array3 = new byte[num];
			Array.Copy(array, 0, array3, 0, num);
			return array3;
		}

		/// <summary>
		/// 根据指定的数据和类型，生成对应的数据
		/// </summary>
		/// <param name="address">地址信息</param>
		/// <param name="typeCode">数据类型</param>
		/// <param name="value">字节值</param>
		/// <param name="length">如果节点为数组，就是数组长度</param>
		/// <param name="isConnectedAddress">是否为连接模式的地址</param>
		/// <returns>CIP的指令信息</returns>
		// Token: 0x06000D3B RID: 3387 RVA: 0x0004E408 File Offset: 0x0004C608
		public static byte[] PackRequestWrite(string address, ushort typeCode, byte[] value, int length = 1, bool isConnectedAddress = false)
		{
			byte[] array = new byte[1024];
			int num = 0;
			array[num++] = 77;
			num++;
			byte[] array2 = AllenBradleyHelper.BuildRequestPathCommand(address, isConnectedAddress);
			array2.CopyTo(array, num);
			num += array2.Length;
			array[1] = (byte)((num - 2) / 2);
			array[num++] = BitConverter.GetBytes(typeCode)[0];
			array[num++] = BitConverter.GetBytes(typeCode)[1];
			array[num++] = BitConverter.GetBytes(length)[0];
			array[num++] = BitConverter.GetBytes(length)[1];
			value.CopyTo(array, num);
			num += value.Length;
			byte[] array3 = new byte[num];
			Array.Copy(array, 0, array3, 0, num);
			return array3;
		}

		/// <summary>
		/// 分析地址数据信息里的位索引的信息，例如a[10]  返回 a 和 10 索引，如果没有指定索引，就默认为0
		/// </summary>
		/// <param name="address">数据地址</param>
		/// <param name="arrayIndex">位索引</param>
		/// <returns>地址信息</returns>
		// Token: 0x06000D3C RID: 3388 RVA: 0x0004E4B8 File Offset: 0x0004C6B8
		public static string AnalysisArrayIndex(string address, out int arrayIndex)
		{
			arrayIndex = 0;
			bool flag = !address.EndsWith("]");
			string result;
			if (flag)
			{
				result = address;
			}
			else
			{
				int num = address.LastIndexOf('[');
				bool flag2 = num < 0;
				if (flag2)
				{
					result = address;
				}
				else
				{
					address = address.Remove(address.Length - 1);
					arrayIndex = int.Parse(address.Substring(num + 1));
					address = address.Substring(0, num);
					result = address;
				}
			}
			return result;
		}

		/// <summary>
		/// 写入Bool数据的基本指令信息
		/// </summary>
		/// <param name="address">地址</param>
		/// <param name="value">值</param>
		/// <returns>报文信息</returns>
		// Token: 0x06000D3D RID: 3389 RVA: 0x0004E524 File Offset: 0x0004C724
		public static byte[] PackRequestWrite(string address, bool value)
		{
			int num;
			address = AllenBradleyHelper.AnalysisArrayIndex(address, out num);
			address = address + "[" + (num / 32).ToString() + "]";
			int value2 = 0;
			int value3 = -1;
			if (value)
			{
				value2 = 1 << num;
			}
			else
			{
				value3 = ~(1 << num);
			}
			byte[] array = new byte[1024];
			int num2 = 0;
			array[num2++] = 78;
			num2++;
			byte[] array2 = AllenBradleyHelper.BuildRequestPathCommand(address, false);
			array2.CopyTo(array, num2);
			num2 += array2.Length;
			array[1] = (byte)((num2 - 2) / 2);
			array[num2++] = 4;
			array[num2++] = 0;
			BitConverter.GetBytes(value2).CopyTo(array, num2);
			num2 += 4;
			BitConverter.GetBytes(value3).CopyTo(array, num2);
			num2 += 4;
			byte[] array3 = new byte[num2];
			Array.Copy(array, 0, array3, 0, num2);
			return array3;
		}

		/// <summary>
		/// 将所有的cip指定进行打包操作。
		/// </summary>
		/// <param name="portSlot">PLC所在的面板槽号</param>
		/// <param name="cips">所有的cip打包指令信息</param>
		/// <returns>包含服务的信息</returns>
		// Token: 0x06000D3E RID: 3390 RVA: 0x0004E61C File Offset: 0x0004C81C
		public static byte[] PackCommandService(byte[] portSlot, params byte[][] cips)
		{
			MemoryStream memoryStream = new MemoryStream();
			memoryStream.WriteByte(178);
			memoryStream.WriteByte(0);
			memoryStream.WriteByte(0);
			memoryStream.WriteByte(0);
			memoryStream.WriteByte(82);
			memoryStream.WriteByte(2);
			memoryStream.WriteByte(32);
			memoryStream.WriteByte(6);
			memoryStream.WriteByte(36);
			memoryStream.WriteByte(1);
			memoryStream.WriteByte(10);
			memoryStream.WriteByte(240);
			memoryStream.WriteByte(0);
			memoryStream.WriteByte(0);
			int num = 0;
			bool flag = cips.Length == 1;
			if (flag)
			{
				memoryStream.Write(cips[0], 0, cips[0].Length);
				num += cips[0].Length;
			}
			else
			{
				memoryStream.WriteByte(10);
				memoryStream.WriteByte(2);
				memoryStream.WriteByte(32);
				memoryStream.WriteByte(2);
				memoryStream.WriteByte(36);
				memoryStream.WriteByte(1);
				num += 8;
				memoryStream.Write(BitConverter.GetBytes((ushort)cips.Length), 0, 2);
				ushort num2 = (ushort)(2 + 2 * cips.Length);
				num += 2 * cips.Length;
				for (int i = 0; i < cips.Length; i++)
				{
					memoryStream.Write(BitConverter.GetBytes(num2), 0, 2);
					num2 = (ushort)((int)num2 + cips[i].Length);
				}
				for (int j = 0; j < cips.Length; j++)
				{
					memoryStream.Write(cips[j], 0, cips[j].Length);
					num += cips[j].Length;
				}
			}
			memoryStream.WriteByte((byte)((portSlot.Length + 1) / 2));
			memoryStream.WriteByte(0);
			memoryStream.Write(portSlot, 0, portSlot.Length);
			bool flag2 = portSlot.Length % 2 == 1;
			if (flag2)
			{
				memoryStream.WriteByte(0);
			}
			byte[] array = memoryStream.ToArray();
			memoryStream.Dispose();
			BitConverter.GetBytes((short)num).CopyTo(array, 12);
			BitConverter.GetBytes((short)(array.Length - 4)).CopyTo(array, 2);
			return array;
		}

		/// <summary>
		/// 将所有的cip指定进行打包操作。
		/// </summary>
		/// <param name="portSlot">PLC所在的面板槽号</param>
		/// <param name="cips">所有的cip打包指令信息</param>
		/// <returns>包含服务的信息</returns>
		// Token: 0x06000D3F RID: 3391 RVA: 0x0004E810 File Offset: 0x0004CA10
		public static byte[] PackCleanCommandService(byte[] portSlot, params byte[][] cips)
		{
			MemoryStream memoryStream = new MemoryStream();
			memoryStream.WriteByte(178);
			memoryStream.WriteByte(0);
			memoryStream.WriteByte(0);
			memoryStream.WriteByte(0);
			bool flag = cips.Length == 1;
			if (flag)
			{
				memoryStream.Write(cips[0], 0, cips[0].Length);
			}
			else
			{
				memoryStream.WriteByte(10);
				memoryStream.WriteByte(2);
				memoryStream.WriteByte(32);
				memoryStream.WriteByte(2);
				memoryStream.WriteByte(36);
				memoryStream.WriteByte(1);
				memoryStream.Write(BitConverter.GetBytes((ushort)cips.Length), 0, 2);
				ushort num = (ushort)(2 + 2 * cips.Length);
				for (int i = 0; i < cips.Length; i++)
				{
					memoryStream.Write(BitConverter.GetBytes(num), 0, 2);
					num = (ushort)((int)num + cips[i].Length);
				}
				for (int j = 0; j < cips.Length; j++)
				{
					memoryStream.Write(cips[j], 0, cips[j].Length);
				}
			}
			memoryStream.WriteByte((byte)((portSlot.Length + 1) / 2));
			memoryStream.WriteByte(0);
			memoryStream.Write(portSlot, 0, portSlot.Length);
			bool flag2 = portSlot.Length % 2 == 1;
			if (flag2)
			{
				memoryStream.WriteByte(0);
			}
			byte[] array = memoryStream.ToArray();
			memoryStream.Dispose();
			BitConverter.GetBytes((short)(array.Length - 4)).CopyTo(array, 2);
			return array;
		}

		/// <summary>
		/// 打包一个读取所有特性数据的报文信息，需要传入slot
		/// </summary>
		/// <param name="portSlot">站号信息</param>
		/// <param name="sessionHandle">会话的ID信息</param>
		/// <returns>最终发送的报文数据</returns>
		// Token: 0x06000D40 RID: 3392 RVA: 0x0004E97C File Offset: 0x0004CB7C
		public static byte[] PackCommandGetAttributesAll(byte[] portSlot, uint sessionHandle)
		{
			byte[] commandSpecificData = AllenBradleyHelper.PackCommandSpecificData(new byte[][]
			{
				new byte[4],
				AllenBradleyHelper.PackCommandService(portSlot, new byte[][]
				{
					new byte[]
					{
						1,
						2,
						32,
						1,
						36,
						1
					}
				})
			});
			return AllenBradleyHelper.PackRequestHeader(111, sessionHandle, commandSpecificData);
		}

		/// <summary>
		/// 根据数据创建反馈的数据信息
		/// </summary>
		/// <param name="data">反馈的数据信息</param>
		/// <param name="isRead">是否是读取</param>
		/// <returns>数据</returns>
		// Token: 0x06000D41 RID: 3393 RVA: 0x0004E9D0 File Offset: 0x0004CBD0
		public static byte[] PackCommandResponse(byte[] data, bool isRead)
		{
			bool flag = data == null;
			byte[] result;
			if (flag)
			{
				byte[] array = new byte[6];
				array[2] = 4;
				result = array;
			}
			else
			{
				byte[][] array2 = new byte[2][];
				int num = 0;
				byte[] array3 = new byte[6];
				array3[0] = (isRead ? 204 : 205);
				array2[num] = array3;
				array2[1] = data;
				result = SoftBasic.SpliceArray<byte>(array2);
			}
			return result;
		}

		/// <summary>
		/// 生成读取直接节点数据信息的内容
		/// </summary>
		/// <param name="service">cip指令内容</param>
		/// <returns>最终的指令值</returns>
		// Token: 0x06000D42 RID: 3394 RVA: 0x0004EA24 File Offset: 0x0004CC24
		public static byte[] PackCommandSpecificData(params byte[][] service)
		{
			MemoryStream memoryStream = new MemoryStream();
			memoryStream.WriteByte(0);
			memoryStream.WriteByte(0);
			memoryStream.WriteByte(0);
			memoryStream.WriteByte(0);
			memoryStream.WriteByte(1);
			memoryStream.WriteByte(0);
			memoryStream.WriteByte(BitConverter.GetBytes(service.Length)[0]);
			memoryStream.WriteByte(BitConverter.GetBytes(service.Length)[1]);
			for (int i = 0; i < service.Length; i++)
			{
				memoryStream.Write(service[i], 0, service[i].Length);
			}
			byte[] result = memoryStream.ToArray();
			memoryStream.Dispose();
			return result;
		}

		/// <summary>
		/// 将所有的cip指定进行打包操作。
		/// </summary>
		/// <param name="command">指令信息</param>
		/// <returns>包含服务的信息</returns>
		// Token: 0x06000D43 RID: 3395 RVA: 0x0004EAC4 File Offset: 0x0004CCC4
		public static byte[] PackCommandSingleService(byte[] command)
		{
			bool flag = command == null;
			if (flag)
			{
				command = new byte[0];
			}
			byte[] array = new byte[4 + command.Length];
			array[0] = 178;
			array[1] = 0;
			array[2] = BitConverter.GetBytes(command.Length)[0];
			array[3] = BitConverter.GetBytes(command.Length)[1];
			command.CopyTo(array, 4);
			return array;
		}

		/// <summary>
		/// 向PLC注册会话ID的报文<br />
		/// Register a message with the PLC for the session ID
		/// </summary>
		/// <returns>报文信息 -&gt; Message information </returns>
		// Token: 0x06000D44 RID: 3396 RVA: 0x0004EB24 File Offset: 0x0004CD24
		public static byte[] RegisterSessionHandle()
		{
			byte[] array = new byte[4];
			array[0] = 1;
			byte[] commandSpecificData = array;
			return AllenBradleyHelper.PackRequestHeader(101, 0U, commandSpecificData);
		}

		/// <summary>
		/// 获取卸载一个已注册的会话的报文<br />
		/// Get a message to uninstall a registered session
		/// </summary>
		/// <param name="sessionHandle">当前会话的ID信息</param>
		/// <returns>字节报文信息 -&gt; BYTE message information </returns>
		// Token: 0x06000D45 RID: 3397 RVA: 0x0004EB4C File Offset: 0x0004CD4C
		public static byte[] UnRegisterSessionHandle(uint sessionHandle)
		{
			return AllenBradleyHelper.PackRequestHeader(102, sessionHandle, new byte[0]);
		}

		/// <summary>
		/// 初步检查返回的CIP协议的报文是否正确<br />
		/// Initially check whether the returned CIP protocol message is correct
		/// </summary>
		/// <param name="response">CIP的报文信息</param>
		/// <returns>是否正确的结果信息</returns>
		// Token: 0x06000D46 RID: 3398 RVA: 0x0004EB6C File Offset: 0x0004CD6C
		public static OperateResult CheckResponse(byte[] response)
		{
			OperateResult result;
			try
			{
				int num = BitConverter.ToInt32(response, 8);
				bool flag = num == 0;
				if (flag)
				{
					result = OperateResult.CreateSuccessResult();
				}
				else
				{
					string msg = string.Empty;
					int num2 = num;
					int num3 = num2;
					if (num3 <= 100)
					{
						switch (num3)
						{
						case 1:
							msg = StringResources.Language.AllenBradleySessionStatus01;
							goto IL_BA;
						case 2:
							msg = StringResources.Language.AllenBradleySessionStatus02;
							goto IL_BA;
						case 3:
							msg = StringResources.Language.AllenBradleySessionStatus03;
							goto IL_BA;
						default:
							if (num3 == 100)
							{
								msg = StringResources.Language.AllenBradleySessionStatus64;
								goto IL_BA;
							}
							break;
						}
					}
					else
					{
						if (num3 == 101)
						{
							msg = StringResources.Language.AllenBradleySessionStatus65;
							goto IL_BA;
						}
						if (num3 == 105)
						{
							msg = StringResources.Language.AllenBradleySessionStatus69;
							goto IL_BA;
						}
					}
					msg = StringResources.Language.UnknownError;
					IL_BA:
					result = new OperateResult(num, msg);
				}
			}
			catch (Exception ex)
			{
				result = new OperateResult(ex.Message);
			}
			return result;
		}

		/// <summary>
		/// 从PLC反馈的数据解析
		/// </summary>
		/// <param name="response">PLC的反馈数据</param>
		/// <param name="isRead">是否是返回的操作</param>
		/// <returns>带有结果标识的最终数据</returns>
		// Token: 0x06000D47 RID: 3399 RVA: 0x0004EC60 File Offset: 0x0004CE60
		public static OperateResult<byte[], ushort, bool> ExtractActualData(byte[] response, bool isRead)
		{
			List<byte> list = new List<byte>();
			int num = 38;
			bool value = false;
			ushort value2 = 0;
			ushort num2 = BitConverter.ToUInt16(response, 38);
			bool flag = BitConverter.ToInt32(response, 40) == 138;
			if (!flag)
			{
				byte b = response[num + 4];
				byte b2 = b;
				byte b3 = b2;
				if (b3 <= 10)
				{
					switch (b3)
					{
					case 0:
						break;
					case 1:
					case 2:
					case 3:
						goto IL_479;
					case 4:
						return new OperateResult<byte[], ushort, bool>
						{
							ErrorCode = (int)b,
							Message = StringResources.Language.AllenBradley04
						};
					case 5:
						return new OperateResult<byte[], ushort, bool>
						{
							ErrorCode = (int)b,
							Message = StringResources.Language.AllenBradley05
						};
					case 6:
						value = true;
						break;
					default:
						if (b3 != 10)
						{
							goto IL_479;
						}
						return new OperateResult<byte[], ushort, bool>
						{
							ErrorCode = (int)b,
							Message = StringResources.Language.AllenBradley0A
						};
					}
					bool flag2 = response[num + 2] == 205 || response[num + 2] == 211;
					if (flag2)
					{
						return OperateResult.CreateSuccessResult<byte[], ushort, bool>(list.ToArray(), value2, value);
					}
					bool flag3 = response[num + 2] == 204 || response[num + 2] == 210;
					if (flag3)
					{
						for (int i = num + 8; i < num + 2 + (int)num2; i++)
						{
							list.Add(response[i]);
						}
						value2 = BitConverter.ToUInt16(response, num + 6);
					}
					else
					{
						bool flag4 = response[num + 2] == 213;
						if (flag4)
						{
							for (int j = num + 6; j < num + 2 + (int)num2; j++)
							{
								list.Add(response[j]);
							}
						}
					}
					goto IL_56C;
				}
				else
				{
					if (b3 == 19)
					{
						return new OperateResult<byte[], ushort, bool>
						{
							ErrorCode = (int)b,
							Message = StringResources.Language.AllenBradley13
						};
					}
					switch (b3)
					{
					case 28:
						return new OperateResult<byte[], ushort, bool>
						{
							ErrorCode = (int)b,
							Message = StringResources.Language.AllenBradley1C
						};
					case 29:
					case 31:
						break;
					case 30:
						return new OperateResult<byte[], ushort, bool>
						{
							ErrorCode = (int)b,
							Message = StringResources.Language.AllenBradley1E
						};
					case 32:
						return new OperateResult<byte[], ushort, bool>
						{
							ErrorCode = (int)b,
							Message = StringResources.Language.AllenBradley20
						};
					default:
						if (b3 == 38)
						{
							return new OperateResult<byte[], ushort, bool>
							{
								ErrorCode = (int)b,
								Message = StringResources.Language.AllenBradley26
							};
						}
						break;
					}
				}
				IL_479:
				return new OperateResult<byte[], ushort, bool>
				{
					ErrorCode = (int)b,
					Message = StringResources.Language.UnknownError
				};
			}
			num = 44;
			int num3 = (int)BitConverter.ToUInt16(response, num);
			int k = 0;
			while (k < num3)
			{
				int num4 = (int)BitConverter.ToUInt16(response, num + 2 + k * 2) + num;
				int num5 = (k == num3 - 1) ? response.Length : ((int)BitConverter.ToUInt16(response, num + 4 + k * 2) + num);
				ushort num6 = BitConverter.ToUInt16(response, num4 + 2);
				ushort num7 = num6;
				ushort num8 = num7;
				if (num8 <= 19)
				{
					switch (num8)
					{
					case 0:
						break;
					case 1:
					case 2:
					case 3:
						goto IL_24C;
					case 4:
						return new OperateResult<byte[], ushort, bool>
						{
							ErrorCode = (int)num6,
							Message = StringResources.Language.AllenBradley04
						};
					case 5:
						return new OperateResult<byte[], ushort, bool>
						{
							ErrorCode = (int)num6,
							Message = StringResources.Language.AllenBradley05
						};
					case 6:
					{
						bool flag5 = response[num + 2] == 210 || response[num + 2] == 204;
						if (flag5)
						{
							return new OperateResult<byte[], ushort, bool>
							{
								ErrorCode = (int)num6,
								Message = StringResources.Language.AllenBradley06
							};
						}
						break;
					}
					default:
						if (num8 == 10)
						{
							return new OperateResult<byte[], ushort, bool>
							{
								ErrorCode = (int)num6,
								Message = StringResources.Language.AllenBradley0A
							};
						}
						if (num8 != 19)
						{
							goto IL_24C;
						}
						return new OperateResult<byte[], ushort, bool>
						{
							ErrorCode = (int)num6,
							Message = StringResources.Language.AllenBradley13
						};
					}
					if (isRead)
					{
						for (int l = num4 + 6; l < num5; l++)
						{
							list.Add(response[l]);
						}
					}
					k++;
					continue;
				}
				if (num8 == 28)
				{
					return new OperateResult<byte[], ushort, bool>
					{
						ErrorCode = (int)num6,
						Message = StringResources.Language.AllenBradley1C
					};
				}
				if (num8 == 30)
				{
					return new OperateResult<byte[], ushort, bool>
					{
						ErrorCode = (int)num6,
						Message = StringResources.Language.AllenBradley1E
					};
				}
				if (num8 == 38)
				{
					return new OperateResult<byte[], ushort, bool>
					{
						ErrorCode = (int)num6,
						Message = StringResources.Language.AllenBradley26
					};
				}
				IL_24C:
				return new OperateResult<byte[], ushort, bool>
				{
					ErrorCode = (int)num6,
					Message = StringResources.Language.UnknownError
				};
			}
			IL_56C:
			return OperateResult.CreateSuccessResult<byte[], ushort, bool>(list.ToArray(), value2, value);
		}

		/// <summary>
		/// CIP命令中的读取数据的服务
		/// </summary>
		// Token: 0x0400038B RID: 907
		public const byte CIP_READ_DATA = 76;

		/// <summary>
		/// CIP命令中的写数据的服务
		/// </summary>
		// Token: 0x0400038C RID: 908
		public const int CIP_WRITE_DATA = 77;

		/// <summary>
		/// CIP命令中的读并写的数据服务
		/// </summary>
		// Token: 0x0400038D RID: 909
		public const int CIP_READ_WRITE_DATA = 78;

		/// <summary>
		/// CIP命令中的读片段的数据服务
		/// </summary>
		// Token: 0x0400038E RID: 910
		public const int CIP_READ_FRAGMENT = 82;

		/// <summary>
		/// CIP命令中的写片段的数据服务
		/// </summary>
		// Token: 0x0400038F RID: 911
		public const int CIP_WRITE_FRAGMENT = 83;

		/// <summary>
		/// CIP指令中读取数据的列表
		/// </summary>
		// Token: 0x04000390 RID: 912
		public const byte CIP_READ_LIST = 85;

		/// <summary>
		/// CIP命令中的对数据读取服务
		/// </summary>
		// Token: 0x04000391 RID: 913
		public const int CIP_MULTIREAD_DATA = 4096;

		/// <summary>
		/// bool型数据，一个字节长度
		/// </summary>
		// Token: 0x04000392 RID: 914
		public const ushort CIP_Type_Bool = 193;

		/// <summary>
		/// byte型数据，一个字节长度，SINT
		/// </summary>
		// Token: 0x04000393 RID: 915
		public const ushort CIP_Type_Byte = 194;

		/// <summary>
		/// 整型，两个字节长度，INT
		/// </summary>
		// Token: 0x04000394 RID: 916
		public const ushort CIP_Type_Word = 195;

		/// <summary>
		/// 长整型，四个字节长度，DINT
		/// </summary>
		// Token: 0x04000395 RID: 917
		public const ushort CIP_Type_DWord = 196;

		/// <summary>
		/// 特长整型，8个字节，LINT
		/// </summary>
		// Token: 0x04000396 RID: 918
		public const ushort CIP_Type_LInt = 197;

		/// <summary>
		/// • Unsigned 8-bit integer, USINT
		/// </summary>
		// Token: 0x04000397 RID: 919
		public const ushort CIP_Type_USInt = 198;

		/// <summary>
		/// Unsigned 16-bit integer, UINT
		/// </summary>
		// Token: 0x04000398 RID: 920
		public const ushort CIP_Type_UInt = 199;

		/// <summary>
		///  Unsigned 32-bit integer, UDINT,
		/// </summary>
		// Token: 0x04000399 RID: 921
		public const ushort CIP_Type_UDint = 200;

		/// <summary>
		///  Unsigned 32-bit integer, ULINT,
		/// </summary>
		// Token: 0x0400039A RID: 922
		public const ushort CIP_Type_ULint = 201;

		/// <summary>
		/// 实数数据，四个字节长度
		/// </summary>
		// Token: 0x0400039B RID: 923
		public const ushort CIP_Type_Real = 202;

		/// <summary>
		/// 实数数据，八个字节的长度
		/// </summary>
		// Token: 0x0400039C RID: 924
		public const ushort CIP_Type_Double = 203;

		/// <summary>
		/// 结构体数据，不定长度
		/// </summary>
		// Token: 0x0400039D RID: 925
		public const ushort CIP_Type_Struct = 204;

		/// <summary>
		/// 字符串数据内容
		/// </summary>
		// Token: 0x0400039E RID: 926
		public const ushort CIP_Type_String = 208;

		/// <summary>
		///  Bit string, 8 bits, BYTE,
		/// </summary>
		// Token: 0x0400039F RID: 927
		public const ushort CIP_Type_D1 = 209;

		/// <summary>
		/// Bit string, 16-bits, WORD
		/// </summary>
		// Token: 0x040003A0 RID: 928
		public const ushort CIP_Type_D2 = 210;

		/// <summary>
		/// Bit string, 32 bits, DWORD
		/// </summary>
		// Token: 0x040003A1 RID: 929
		public const ushort CIP_Type_D3 = 211;

		/// <summary>
		/// 二进制数据内容
		/// </summary>
		// Token: 0x040003A2 RID: 930
		public const ushort CIP_Type_BitArray = 211;
	}
}
